Set ANSI_NULLS ON
Set QUOTED_IDENTIFIER ON
GO

/*Drop stored procedures if they exists */

IF EXISTS (SELECT name FROM sysobjects WHERE name = 'eliminarProyecto' and type = 'P')

	BEGIN
		DROP PROCEDURE eliminarProyecto
	END

GO

CREATE PROCEDURE eliminarProyecto ( @idProyecto INT ,	 
								  @outParam INT OUTPUT						
								)
AS
	DECLARE @out AS INT;
	SET @out = -1;
	
	BEGIN TRANSACTION;
	
		BEGIN TRY

			DELETE FROM MATERIAL_REAL WHERE MATERIAL_REAL.IDPROYECTO = @idProyecto;
		
			DELETE FROM ESTIMACIONMATERIAL WHERE ESTIMACIONMATERIAL.IDPROYECTO = @idProyecto;
			
			DELETE FROM PROYECTO WHERE PROYECTO.IDPROYECTO = @idProyecto;
			
			SET @out = 1;
			
			SET @outParam = @out;
			
			COMMIT TRANSACTION; 
		
		END TRY
		
		BEGIN CATCH
		
			SET @outParam = @out;
			
			ROLLBACK TRANSACTION;
		
		END CATCH;
	
GO